package pers.youqu.common.advice;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import pers.youqu.common.exception.YqException;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/3 21:13
 * @Description TODO
 */
@ControllerAdvice
public class ControllerExceptionAdvice {
    /**
     * 统一异常处理方法，@ExceptionHandler(RuntimeException.class)声明这个方法处理RuntimeException这样的异常
     *
     * @param e 捕获到的异常
     * @return 返回给页面的状态码和信息
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleLyException(RuntimeException e) {
        if (e instanceof YqException) {
            YqException yqException = (YqException) e;
            return ResponseEntity.status(yqException.getStatus()).body(e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
    }
}
